#!/bin/bash
HOME="/home"
VERSION="5.7.38"
MYSQL_FILES="mysql-${VERSION}.tar.gz"
MYSQL_FILES_DIR="mysql-${VERSION}"
MYSQL_URL="https://mirrors.aliyun.com/mysql/MySQL-5.7/$MYSQL_FILES"
MYSQL_PREFIX_TEMP="/usr/local"
MYSQL_PREFIX="${MYSQL_PREFIX_TEMP}/mysql57"
MYSQL_BOOST_URL="https://mirrors.aliyun.com/mysql/MySQL-5.7/mysql-boost-${VERSION}.tar.gz"

#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}

function decompressBoostDeal() {
    if [ ! -d ${MYSQL_PREFIX_TEMP}/mysql-${VERSION}-boost ]; then
        tar -xzvf mysql-boost-${VERSION}.tar.gz -C ${MYSQL_PREFIX_TEMP} && mv ${MYSQL_PREFIX_TEMP}/mysql-${VERSION} ${MYSQL_PREFIX_TEMP}/mysql-${VERSION}-boost
    fi
}

function decompressBoost() {
    cd "$HOME"
    if [ ! -f "$HOME/mysql-boost-${VERSION}.tar.gz" ]; then
        wget -c $MYSQL_BOOST_URL
        decompressBoostDeal
    else
        decompressBoostDeal
    fi
}

function writeMycnf() {
    FILE_LIST=(/etc/my.cnf $MYSQL_PREFIX/my.cnf)
    for ((i = 0; i <= ${#FILE_LIST[@]} - 1; i++)); do
        cat >${FILE_LIST[i]} <<EOF
[client]
port=3306
default-character-set=utf8mb4
[mysqld]
basedir=$MYSQL_PREFIX
datadir=$MYSQL_PREFIX/data/mysql57
port=3306
# 允许最大连接数
max_connections=3000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#lower_case_table_name=1
max_allowed_packet=64M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER
explicit_defaults_for_timestamp=true
EOF
    done
}

function installMysql() {
    rpm -q tar &>/dev/null
    if [ $? -ne 0 ]; then
        yum install tar -y
    fi
    rpm -q gcc &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc -y
    fi
    rpm -q gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum install gcc-c++ -y
    fi
    rpm -q openssl &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl -y
    fi
    rpm -q openssl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install openssl-devel -y
    fi
    rpm -q autoconf &>/dev/null
    if [ $? -ne 0 ]; then
        yum install autoconf -y
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi
    rpm -q cmake &>/dev/null
    if [ $? -ne 0 ]; then
        yum install cmake -y
    fi
    rpm -q ncurses &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses -y
    fi
    rpm -q ncurses-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-devel -y
    fi
    rpm -q ncurses-libs &>/dev/null
    if [ $? -ne 0 ]; then
        yum install ncurses-libs -y
    fi
    createUserGroupAndUser mysql mysql
    if [ ! -d "$HOME" ]; then
        mkdir $HOME
    fi
    # 安装mysql-boost
    decompressBoost

    cd "$HOME"
    if [ ! -f "$HOME/$MYSQL_FILES" ]; then
        wget -c $MYSQL_URL && tar -xzvf $MYSQL_FILES
    fi
    if [ ! -d "$HOME/$MYSQL_FILES_DIR" ]; then
        tar -xzvf $MYSQL_FILES
    fi
    cd $MYSQL_FILES_DIR
    cmake . -DCMAKE_INSTALL_PREFIX=$MYSQL_PREFIX \
        -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
        -DMYSQL_DATADIR=${MYSQL_PREFIX_TEMP}/data/mysql57 \
        -DSYSCONFDIR=/etc \
        -DMYSQL_USER=mysql \
        -DMYSQL_TCP_PORT=3306 \
        -DWITH_XTRADB_STORAGE_ENGINE=1 \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_PARTITION_STORAGE_ENGINE=1 \
        -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
        -DWITH_MYISAM_STORAGE_ENGINE=1 \
        -DWITH_READLINE=1 \
        -DENABLED_LOCAL_INFILE=1 \
        -DWITH_EXTRA_CHARSETS=1 \
        -DDEFAULT_CHARSET=utf8mb4 \
        -DDEFAULT_COLLATION=utf8mb4_general_ci \
        -DEXTRA_CHARSETS=all \
        -DWITH_BIG_TABLES=1 \
        -DWITH_DEBUG=0 \
        -DDOWNLOAD_BOOST=1 \
        -DWITH_BOOST=${MYSQL_PREFIX_TEMP}/mysql-${VERSION}-boost
    make && make install
    if [ $? -eq 0 ]; then
        #msyql的配置文件
        writeMycnf
        #msyql的开机启动
        /bin/cp support-files/mysql.server /etc/init.d/mysqld
        chmod +x /etc/init.d/mysqld
        chkconfig --add /etc/init.d/mysqld
        chkconfig mysqld on
        #初始化数据库
        cd $MYSQL_PREFIX && mkdir -p ${MYSQL_PREFIX}/data/mysql57 && chown -R mysql:mysql ./ &&  ./bin/mysqld --defaults-file=${MYSQL_PREFIX}/my.cnf --basedir=${MYSQL_PREFIX}/ --datadir=${MYSQL_PREFIX}/data/mysql57/ --user=mysql --initialize
        #软链接
        ln -s $MYSQL_PREFIX/bin/mysql /usr/bin/mysql
        echo -e "\n\033[32m———————————————–\033[0m"
        echo -e "\033[32mServer Install Success !\033[0m"
        echo -e "\033[32mvim /etc/init.d/mysqld\033[0m"
        echo -e "\033[32mbasedir=$MYSQL_PREFIX\033[0m"
        echo -e "\033[32mdatadir=$MYSQL_PREFIX/data/mysql57\033[0m"
        echo -e "\033[32mservice mysqld stop|restart|start|status\033[0m"
        echo -e "\033[32m登录进来了用SET PASSWORD = PASSWORD('123456');修改密码 \033[0m"
    else
        echo -e "\033[32mMake or Make install ERROR,(rm CMakeCache.txt解决安装依赖重新安装)\033[0m"
        exit 0
    fi
}

installMysql
